import typescript from '@rollup/plugin-typescript';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import dts from 'rollup-plugin-dts';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import path from 'path';
import fs from 'fs';



// 外部依赖列表 - 这些依赖不会被打包，需要用户自己安装
const external = [
  // Node.js 内置模块
  'fs', 'path', 'crypto', 'util', 'os', 'stream', 'events', 'url', 'querystring',
  'http', 'https', 'zlib', 'buffer',
  // 第三方依赖
  'vue', 'pinia', 'codemirror', '@vueuse/core', 'tslib',
  // Node.js 模块前缀
  'node:path', 'node:process', 'node:fs', 'node:util', 'node:stream',
  // 大型依赖
  'playwright', 'puppeteer'
];



// 通用插件配置
const getPlugins = (outDir, declaration = false) => [
  // 路径别名配置
  alias({
    entries: [
      { find: '@', replacement: path.resolve('./src') }
    ]
  }),

  // JSON 文件支持
  json(),

  resolve({
    browser: true,
    preferBuiltins: false,
    extensions: ['.js', '.ts', '.json']
  }),

  commonjs({
    include: ['node_modules/**']
  }),

  typescript({
    tsconfig: './tsconfig.json',
    outDir,
    declaration,
    declarationDir: declaration ? 'dist/types' : undefined,
    declarationMap: declaration,
    compilerOptions: {
      // 优化编译性能
      incremental: true,
      skipLibCheck: true,
      // 避免重复的类型检查
      isolatedModules: true
    }
  })
];

// 检查是否为快速构建模式
const isFastBuild = process.env.BUILD_MODE === 'fast';

// 子模块配置
const subModules = [
  { input: 'src/core/storage/index.ts', name: 'storage' },
  { input: 'src/core/platforms/index.ts', name: 'platforms' },
  { input: 'src/core/rpa/index.ts', name: 'rpa' }
];

const config = [
  // 主入口 - ESM build
  {
    input: 'src/index.ts',
    output: {
      file: 'dist/esm/index.js',
      format: 'es',
      sourcemap: !isFastBuild,
      inlineDynamicImports: true
    },
    plugins: getPlugins('dist/esm'),
    external
  },

  // 子模块 - ESM builds
  ...subModules.map(({ input, name }) => ({
    input,
    output: {
      file: `dist/esm/core/${name}/index.js`,
      format: 'es',
      sourcemap: !isFastBuild,
      inlineDynamicImports: true
    },
    plugins: getPlugins(`dist/esm/core/${name}`),
    external
  })),

  // 快速模式下跳过CJS构建
  ...(isFastBuild ? [] : [
    // 主入口 - CJS build
    {
      input: 'src/index.ts',
      output: {
        file: 'dist/cjs/index.cjs',
        format: 'cjs',
        sourcemap: true,
        exports: 'named',
        inlineDynamicImports: true
      },
      plugins: getPlugins('dist/cjs'),
      external
    },

    // 子模块 - CJS builds
    ...subModules.map(({ input, name }) => ({
      input,
      output: {
        file: `dist/cjs/core/${name}/index.cjs`,
        format: 'cjs',
        sourcemap: true,
        exports: 'named',
        inlineDynamicImports: true
      },
      plugins: getPlugins(`dist/cjs/core/${name}`),
      external
    }))
  ]),

  // 主入口类型定义
  {
    input: 'src/index.ts',
    output: {
      file: 'dist/types/index.d.ts',
      format: 'es'
    },
    plugins: [dts()],
    external
  },

  // 子模块类型定义
  ...subModules.map(({ input, name }) => ({
    input,
    output: {
      file: `dist/types/core/${name}/index.d.ts`,
      format: 'es'
    },
    plugins: [dts()],
    external
  }))
];

export default config;
